package com.smt.take.presenter

import android.util.Log
import com.smt.take.model.beans.OrderBean
import com.smt.take.model.net.BaseResponse
import com.smt.take.ui.fragment.OrderFragment
import rx.Observer
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers

class OrderFragmentPresenter(private val orderFragment: OrderFragment?) :
    BasePresenter<MutableList<OrderBean>>(orderFragment?.context) {

    fun getOrderList(userId: String) {
        val observable = takeOutService.getOrderListByRxjava(userId)
        //orderCall.enqueue(callback)
//        observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
//            .subscribe(object : Observer<BaseResponse<MutableList<OrderBean>>> {
//                override fun onCompleted() {
//
//                }
//
//                override fun onError(e: Throwable?) {
//                    Log.e("rxjava", e?.localizedMessage.toString())
//                }
//
//                override fun onNext(response: BaseResponse<MutableList<OrderBean>>?) {
//                    if (response != null) {
//                        setDataToUI(response.data)
//                    }
//                }
//
//            })

        observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                setDataToUI(it.data)
            },{
                Log.e("rxjava", it?.localizedMessage.toString())
            },{
                Log.e("rxjava", "完成")
            })
    }

    override fun setDataToUI(data: MutableList<OrderBean>?) {
        if (data != null) {
            orderFragment?.onOrderSuccess(data)
        } else {
            orderFragment?.onOrderFailed()
        }
    }
}